package com.attilax.facedetection;

import java.util.Map;
import java.util.function.Function;

import org.apache.commons.io.FileUtils;
import org.opencv.core.Core;
import org.opencv.core.CvException;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.objdetect.CascadeClassifier;

import com.alibaba.fastjson.JSON;
import com.attilax.img.util.OpencvUtil;
import com.attilax.io.FileExistEx;
import com.attilax.io.FileUtil;
import com.attilax.io.dirx;
import com.attilax.io.filex;
import com.attilax.io.filexEx;
import com.attilax.io.pathx;
import com.attilax.util.Mapx;
import com.attilax.util.mapUtil;
import com.google.common.collect.Maps;

/**
 * v2 org code s323
 * 
 * @author attilax
 *
 */
@SuppressWarnings("all")
public class FaceDetector {
	public static int num = 0;

	/**
	 * System.load( "C:\\progrm\\opencv\\build\\java\\x64\\opencv_java2413.dll"
	 * ); // System.loadLibrary(Core.NATIVE_LIBRARY_NAME); //
	 * "C:\\progrm\\opencv\\sources\\data\\haarcascades_GPU\\haarcascade_frontalface_alt.xml";
	 * //FaceDetector.class.getResource("haarcascade_frontalface_alt.xml").
	 * getPath(); //meitu syosyo zosh use jeig d .. zaidren d blog shwa yao use
	 * haarcascades hamyar d ..
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		OpencvUtil.ini();
		CascadeClassifier faceDetector_CascadeClassifier = getClassifier();

		System.out.println("\nRunning FaceDetector");
		String dir = "C:\\Users\\attilax\\Documents\\r2017 cp pic bek v8 s229\\cp pic r2017 q4";
		dirx.traveV3(dir, new Function<String, Object>() {

			@Override
			public Object apply(String f) {
				num++;
				System.out.println("num:" + num);

				if (f.contains("faced4dbg"))
					return null;
				if (f.endsWith(".txt"))
					return null;
				System.out.println(f);

				try {
					MatOfRect faceDetectionsRzt_MatOfRect = Single(f, faceDetector_CascadeClassifier);
					if (faceDetectionsRzt_MatOfRect.toArray().length == 0)
						FileUtil.move(f, "c:\\fcrzt", dir);
					Mapx m = Mapx.newx().put("f", f).put("fcnum", faceDetectionsRzt_MatOfRect.toArray().length);

					System.out.println(JSON.toJSONString(m.mp, true));
					return null;
				} catch (CvException e) {
					FileUtil.move_sf(f, "c:\\fcrzt_cvex", dir);

				} catch (Exception e) {
					e.printStackTrace();
				}
				return f;

			}
		});

		// tsetSingle();
	}

	private static void tsetSingle() {
		CascadeClassifier faceDetector_CascadeClassifier = getClassifier();
		String img_path = "D:\\0bar\\g2.jpg";
		Mat image_mat = Highgui.imread(img_path);

		MatOfRect faceDetectionsRzt_MatOfRect = faceDetectRzt(faceDetector_CascadeClassifier, image_mat);

		out4ddbg(img_path, image_mat, faceDetectionsRzt_MatOfRect);
	}

	private static MatOfRect Single(String img_path, CascadeClassifier faceDetector_CascadeClassifier) {
		// CascadeClassifier faceDetector_CascadeClassifier = getClassifier();
		// String img_path = "D:\\0bar\\g2.jpg";
		Mat image_mat = Highgui.imread(img_path);

		MatOfRect faceDetectionsRzt_MatOfRect = faceDetectRzt(faceDetector_CascadeClassifier, image_mat);

		out4ddbg(img_path, image_mat, faceDetectionsRzt_MatOfRect);
		return faceDetectionsRzt_MatOfRect;
	}

	private static CascadeClassifier getClassifier() {
		String path = pathx.prjPathV2() + "\\dll\\haarcascade_frontalface_alt.xml";

		CascadeClassifier faceDetector = new CascadeClassifier(path);
		return faceDetector;
	}

	private static MatOfRect faceDetectRzt(CascadeClassifier faceDetector, Mat image_mat) {
		MatOfRect faceDetections = new MatOfRect();
		faceDetector.detectMultiScale(image_mat, faceDetections);

		return faceDetections;
	}

	private static void out4ddbg(String img, Mat image_mat, MatOfRect faceDetectionsRzt_MatOfRect) {

		for (Rect rect : faceDetectionsRzt_MatOfRect.toArray()) {
			Core.rectangle(image_mat, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
					new Scalar(0, 0, 255));
		}

		String filename = filexEx.addTimestampNSuffix(img, "_faced4dbg");
		System.out.println("out4ddbg::" + String.format("Writing %s", filename));
		Highgui.imwrite(filename, image_mat);
	}
}